/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Entities.PcroomlistFacade;
import classes.Seat;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sarah
 */
@WebServlet(name = "PcroomServlet", urlPatterns = {"/PcroomServlet"})
public class PcroomServlet extends HttpServlet {

    @EJB
    private PcroomlistFacade pcroomlistFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String NAME = new String(request.getParameter("NAME").getBytes("8859_1"), "UTF-8");
            String ADDRESS = new String(request.getParameter("ADDRESS").getBytes("8859_1"), "UTF-8");
            String TELEPHONE = request.getParameter("TEL");
            String LAT = request.getParameter("LAT");
            String LON = request.getParameter("LON");
            double lat = Double.parseDouble(LAT);
            double lon = Double.parseDouble(LON);
            int s[][] = new int[10][10];
            String NUM;
            ArrayList<Seat> SEAT = new ArrayList<Seat>();

            for (int i = 1; i <= 10; i++) {
                for (int j = 1; j <= 10; j++) {
                    NUM = request.getParameter(i + "," + j);
                    if (NUM.equals("")) {
                        s[i - 1][j - 1] = 0;
                    } else {
                        s[i - 1][j - 1] = Integer.parseInt(NUM);
                    }
                    Seat seat = new Seat(j, i, s[i-1][j-1]);
                    seat.setIsAvailable(Boolean.TRUE);
                    SEAT.add(seat);
                }
            }

            pcroomlistFacade.addPcroom(NAME, lat, lon, ADDRESS, TELEPHONE, SEAT);
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>PC방 등록 완료</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>PC방 등록 완료</h1>");
            out.println("<p>NAME: " + NAME + "</p>");
            out.println("<p>ADDRESS: " + ADDRESS + "</p>");
            out.println("<p>TELEPHONE: " + TELEPHONE + "</p>");
            for (int i = 1; i <= 10; i++) {
                out.println("<p>");
                for (int j = 1; j <= 10; j++) {
                    out.print(s[i - 1][j - 1] + " ");
                }
                out.println("</p>");
            }
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
